<?php

namespace App\Backend\Service;

use App\Models\Userreport;
use App\Exceptions\InvalidRequestException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use App\Backend\Service\BaseService;
class UserreportService extends BaseService
{

    private $model;

    public function __construct(Userreport $model)
    {
        parent ::__construct();
        $this->model = $model;
    }

     /**
     * 列表
     */
    public function getList($request,$data=[])
    {
        $page = intval($request->input('page')) ?$request->input('page') :$this->cpage;
        $limit = intval($request->input('limit')) ?$request->input('limit') :$this->climit;
        $offset = ($page-1)*$limit;
        $where=' where 1=1 ';

        if($data['start_date']){
            $where.="   and A.update_time >= '{$data['start_date']}'  ";
        }
        if($data['end_date']){
            $where.="   and A.update_time <= '{$data['end_date']}'  ";
        }
        $type=$data['type'];
        switch ($type) {
            case '1'://豆瓣
                
                $sql="SELECT A.*,B.people_id as people_id,B.content as content  FROM `module_douban_post`  as A  INNER JOIN   `module_user_report` as B ON A.unique_id =B.unique_id {$where} LIMIT {$offset}, {$limit} ";
                $sql1="select count(1) as num from `module_user_report`  where type='1' ";
                break;
            case '2'://爆料
                
                $sql="SELECT A.*,B.people_id as people_id,B.content as content  FROM `module_baoliao_post`  as A  INNER JOIN   `module_user_report` as B ON A.unique_id =B.unique_id {$where} LIMIT {$offset}, {$limit} ";
                $sql1="select count(1) as num from `module_user_report`  where type='2' ";
                break;

            case '3'://逛逛
                
                $sql="SELECT A.*,B.people_id as people_id,B.content as content  FROM `module_douban_post`  as A  INNER JOIN   `module_user_report` as B ON A.unique_id =B.unique_id {$where} LIMIT {$offset}, {$limit} ";
                $sql1="select count(1) as num from `module_user_report`  where type='1' ";
                break;

            default:
        }


        //die($sql);
        $data=DB::select($sql);
        $data = array_map('get_object_vars', $data);

        $count=DB::select($sql1);
        $count=$count['0']->num;

        $count = count($data);

        $list= array_slice($data, $offset, $limit);
        $total_page=count($list);
        $result=[
            'page'=>$page,
            'limit'=>$limit,
            'count'=>$count,
            'total_page'=>$total_page,
            'data'=>$data,
        ];
        return $result;
    }

    /**
     * 详情
     */

    public function detail($request,$unique_id){
       $where = [
           ['unique_id','=',$unique_id],
        ];
        $list = $this->model->where($where)->first();;
        if(!empty($list)){
            $list['content_books'] =json_decode($list['content_books'],true);
            $list['images'] =json_decode($list['images'],true);   
            $list['guanlian_shop'] =json_decode($list['guanlian_shop'],true);   
        }

        
        return $list;
    }
      /**
     * 添加
     */
    public function add($request,$data)
    {
        $data['updator'] = $data['creator']=$this->userId;
        $res =  $this->model->insert($data);
        if($res){
            $this->success();
        }else{
            $this->error('添加失败');
        }

    }

    /**
     * 编辑
     */
    public function edit($request,$data,$unique_id)
    {
        $res = $this->model->where('unique_id',$unique_id)->first();
        if(!$res) {
            $this->error('内容不存在');
        }
        $check_result=$data['check_result'];
        //同意 check_status
        if($check_result=='1'){
            $data['status']='1';
            $data['check_status']='3';

        }
        //拒绝
        if($check_result=='3'){
            $data['status']='2';
            $data['check_status']='3';


        }
        $data['update_time'] = date("Y-m-d H:i:s");

        $res =  $this->model->where('unique_id',$unique_id)->update($data);
        if(!$res){
            $this->error('修改失败');
        }else{
            $this->success();
        }
    }   

     /**
     * 删除
     */
    public function del($ids)
    {
        $res =  $this->model->whereIn('unique_id',$ids)->delete();
        if(!$res){
            $this->error('删除失败');
        }else{
            $this->success();
        }
    }   
    


}
